package com.googlecode.tabletags.components;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.util.UrlHelper;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * Render an HTML sortable column.
 */
@StrutsTag(name = "sortableHeader", tldTagClass = "com.googlecode.tabletags.views.jsp.ui.SortableHeaderTag", description = "Renders a single sortable table column header")
public class SortableHeader extends AbstractURLComponent {
    /**
     * The name of the default template for the SortableHeaderTag.
     */
    final public static String TEMPLATE = "sortableheader";
    private HttpServletRequest req;
    private HttpServletResponse res;

    // sort column attributes
    protected String sortKey;
    protected String sortOrder;
    protected String sortable;

    /**
     * @see UIBean
     * @param stack
     * @param request
     * @param response
     */
    public SortableHeader(ValueStack stack, HttpServletRequest request,
        HttpServletResponse response) {
        super(stack, request, response);
        this.req = request;
        this.res = response;
    }

    /**
     * @see com.opensymphony.webwork.components.UIBean#getDefaultTemplate()
     */
    protected String getDefaultTemplate() {
        return TEMPLATE;
    }

    /**
     * @see com.opensymphony.webwork.components.UIBean#evaluateExtraParams()
     */
    protected void evaluateExtraParams() {
        super.evaluateExtraParams();

        if(sortKey != null) {
            sortKey = findString(sortKey);
        }

        if(sortOrder != null) {
            sortOrder = (String) findValue(sortOrder);
        }

        if(sortable != null) {
            addParameter("sortable", findValue(sortable, Boolean.class));
        }

        addParameter("sortOrder", sortOrder);

        if((value == null) && (name != null)) {
            value = (String) findValue(name);
        } else {
            value = (String) findValue(value);
        }

        boolean showIndicator = (sortKey != null) && sortKey.equals(value);

        addParameter("showIndicator", toBoolean(showIndicator));
        
        Table table = (Table) this.findAncestor(Table.class);
        if(table != null) {
            String clientSort = table.getClientSort();
            if(clientSort != null) {
                Boolean isClientSort = (Boolean) findValue(clientSort, Boolean.class);
                addParameter("clientSort", isClientSort);
                parameters.put("sortable", true);
            }
        }
    }
    
    protected void populateExtraParams(Map<String, String> urlParams) {
        if((sortKey != null) && (name != null)) {
            urlParams.put(name, sortKey);
        }
    }

    /**
     * @param boolValue
     * @return the Boolean value of the boolean.
     */
    private Boolean toBoolean(boolean boolValue) {
        return boolValue ? Boolean.TRUE : Boolean.FALSE;
    }

    protected Class getValueClassType() {
        return String.class;
    }

    @StrutsTagAttribute(description = "the sort order (ascending or descending)")
    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    @StrutsTagAttribute(description = "the key for this sort column")
    public void setSortKey(String sortKey) {
        this.sortKey = sortKey;
    }

    @StrutsTagAttribute(description = "whether or not this column is sortable")
    public void setSortable(String sortable) {
        this.sortable = sortable;
    }

    @StrutsTagAttribute(description = "TODO: fix superclass tld generation")
    @Override
    public void setValue(String arg0) {
        super.setValue(arg0);
    }

    @StrutsTagAttribute(description = "TODO: fix superclass tld generation")
    @Override
    public void setLabel(String arg0) {
        super.setLabel(arg0);
    }

    @StrutsTagAttribute(description = "TODO: fix superclass tld generation")
    @Override
    public void setName(String arg0) {
        super.setName(arg0);
    }

    @StrutsTagAttribute(description = "TODO: fix superclass tld generation")
    @Override
    public void setTitle(String arg0) {
        super.setTitle(arg0);
    }

    public String getSortable() {
        return sortable;
    }
    
    public String getTitle() {
        return title;
    }

    public String getUrl() {
        return url;
    }

    public String getAction() {
        return action;
    }

    public boolean isEncode() {
        return encode;
    }

    public String getMethod() {
        return method;
    }

    public String getNamespace() {
        return namespace;
    }

    public String getScheme() {
        return scheme;
    }

    public String getSortKey() {
        return sortKey;
    }

    public String getOrder() {
        return sortOrder;
    }
    
    public String getSortOrder() {
        return sortOrder;
    }
    
    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }
}
